home *** CD-ROM | disk | FTP | other *** search
- ///--------------------------------------------------------------------------------------
- // Simple.c
- ///--------------------------------------------------------------------------------------
-
- #include <SWIncludes.h> // Automatically include all SpriteWorld headers
- #include <SWGameUtils.h>
- #include <SWFPSReport.h>
-
- #include "SWApplication.h"
- #include "Simple.h"
-
-
- #define kFullScreenWindow false // Set to true if you want a window larger than 640x480
- #define kInterlacedMode false // You can use interlacing even with non-scrolling animations
- #define kSyncToVBL false // Syncs the animation to the VBL
- #define kWorldRectInset 0 // Makes the SpriteWorld smaller than the window.
- #define kMaxFPS 30 // Keep the animation from going too fast
-
- #define kNumSprites 10
- #define kMaxSpriteMoveDelta 8
-
- #define kUseShadows true // Adds shadows below each Sprite!
- #define kShadowOffset 13 // Number of pixels to offset each shadow
-
-
- SpriteWorldPtr gSpriteWorldP;
- SpriteLayerPtr gSpriteLayerP, gShadowSpriteLayerP;
- SpritePtr gMasterBallSpriteP;
- WindowPtr gWindowP;
-
-
- ///--------------------------------------------------------------------------------------
- // main
- ///--------------------------------------------------------------------------------------
-
- void main(void)
- {
- Initialize(kNumberOfMoreMastersCalls);
- Randomize();
-
- if (SWHasSystem7())
- {
- HideControlStrip();
- SWSetCleanUpFunction(MyCleanUpFunction);
-
- CreateWindow();
- SetUpSpriteWorld();
- CreateBallSprite();
-
- AddSprites();
- RunAnimation();
- CleanUp();
-
- RestoreControlStrip();
- }
- else
- {
- CantRunOnThisMachine();
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CreateWindow
- ///--------------------------------------------------------------------------------------
-
- void CreateWindow(void)
- {
- Rect windRect;
- RgnHandle mBarUpdateRgn;
-
- gWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
-
- if (gWindowP != NULL)
- {
- if (kFullScreenWindow == true)
- {
- SizeWindow(gWindowP, qd.screenBits.bounds.right,
- qd.screenBits.bounds.bottom, false);
- MoveWindow(gWindowP, 0, 0, false);
- }
- else
- {
- MoveWindow(gWindowP, 0, 0, false);
- windRect = gWindowP->portRect;
-
- // Make sure the window fits on the screen
- if (windRect.bottom > qd.screenBits.bounds.bottom ||
- windRect.right > qd.screenBits.bounds.right)
- {
- // Make it smaller so it fits on the screen
- SizeWindow(gWindowP, qd.screenBits.bounds.right,
- qd.screenBits.bounds.bottom, false);
- MoveWindow(gWindowP, 0, 0, false);
- }
- else
- {
- // Center window in screen
- CenterRect(&windRect, &qd.screenBits.bounds);
- MoveWindow(gWindowP, windRect.left, windRect.top, false);
- }
- }
-
- ShowWindow(gWindowP);
- SetPort(gWindowP);
- mBarUpdateRgn = SWHideMenuBar(gWindowP); // Must be done *after* showing window!
- EraseRgn(mBarUpdateRgn);
- }
- else
- CantFindResource();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // SetUpSpriteWorld
- ///--------------------------------------------------------------------------------------
-
- void SetUpSpriteWorld(void)
- {
- OSErr err;
- Rect worldRect;
- PixPatHandle pixPatH;
-
- SetCursor(*GetCursor(watchCursor));
-
- // Initialize the SpriteWorld package
- err = SWEnterSpriteWorld();
- FatalError(err);
-
- worldRect = gWindowP->portRect;
- InsetRect(&worldRect, kWorldRectInset, kWorldRectInset);
-
- // Create the SpriteWorld
- err = SWCreateSpriteWorldFromWindow(&gSpriteWorldP, (CWindowPtr)gWindowP,
- &worldRect, NULL, 0);
- FatalError(err);
-
- // Create the Sprite Layers
- err = SWCreateSpriteLayer(&gSpriteLayerP);
- FatalError(err);
- err = SWCreateSpriteLayer(&gShadowSpriteLayerP);
- FatalError(err);
-
- // Put the pieces together and lock the SpriteWorld.
- // Note: since we are locking the SpriteWorld before adding the sprites,
- // we must make sure to lock each sprite individually when creating them.
- SWAddSpriteLayer(gSpriteWorldP, gShadowSpriteLayerP);
- SWAddSpriteLayer(gSpriteWorldP, gSpriteLayerP);
- SWLockSpriteWorld(gSpriteWorldP);
-
-
- // Use BlitPixie for the screen and offscreen DrawProcs.
- if (gSpriteWorldP->pixelDepth >= 8) // 8-bit, 16-bit, and 32-bit blitter
- {
- SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixieRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixieRectDrawProc);
- }
- else if ( SW_68K ) // Use AllBit blitter when in depths lower than 8-bits on 68k.
- {
- SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixieAllBitRectDrawProc);
- SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixieAllBitRectDrawProc);
- }
-
- if (kInterlacedMode)
- {
- SWSetFrameInterlacingMode(gSpriteWorldP->workFrameP, true, kSkipOddLines);
- SWSetFrameInterlacingMode(gSpriteWorldP->windowFrameP, true, kSkipOddLines);
- }
-
- SWSetPortToWorkArea(gSpriteWorldP);
- PaintRect(&gSpriteWorldP->backRect);
-
- // Draw the background
- SWSetPortToBackground(gSpriteWorldP);
-
- if (gSpriteWorldP->pixelDepth == 1)
- pixPatH = GetPixPat(129); // B&W dithered background
- else
- pixPatH = GetPixPat(128); // Color
-
- if (pixPatH != NULL)
- {
- FillCRect(&gSpriteWorldP->backRect, pixPatH);
- DisposePixPat(pixPatH);
- }
-
- // Set up other miscellaneous options
- SWSetSpriteWorldMaxFPS(gSpriteWorldP, kMaxFPS);
- SWSyncSpriteWorldToVBL(gSpriteWorldP, kSyncToVBL);
- SWSetCleanUpSpriteWorld(gSpriteWorldP);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CreateBallSprite - load the master ball sprite and prepare it for cloning later
- ///--------------------------------------------------------------------------------------
-
- void CreateBallSprite(void)
- {
- OSErr err;
-
- err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &gMasterBallSpriteP, NULL,
- 128, 1, kFatMask);
- FatalError(err);
-
- // Set up the sprite (remember that it must be locked!)
- SWLockSprite(gMasterBallSpriteP);
- SWSetSpriteMoveBounds(gMasterBallSpriteP, &gSpriteWorldP->backRect);
- SWSetSpriteMoveProc(gMasterBallSpriteP, BallSpriteMoveProc);
-
- // Set the sprite's drawProc
- if (gSpriteWorldP->pixelDepth >= 8) // 8-bit, 16-bit, and 32-bit blitter
- {
- SWSetSpriteDrawProc(gMasterBallSpriteP, BlitPixieMaskDrawProc);
- }
- else if ( SW_68K ) // 68k blitter for any depth
- {
- SWSetSpriteDrawProc(gMasterBallSpriteP, BlitPixieAllBitMaskDrawProc);
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // AddSprites - clone the master sprite, and add the clones to the SpriteWorld
- ///--------------------------------------------------------------------------------------
-
- void AddSprites(void)
- {
- SpritePtr newSpriteP, shadowSpriteP;
- short spriteNum;
- OSErr err;
-
- for (spriteNum = 0; spriteNum < kNumSprites; spriteNum++)
- {
- // Clone the master sprite. The clone doesn't need to be locked,
- // since the master sprite was already locked.
- err = SWCloneSprite(gMasterBallSpriteP, &newSpriteP, NULL);
- FatalError(err);
-
- SWAddSprite(gSpriteLayerP, newSpriteP);
-
- SWSetSpriteLocation(newSpriteP,
- GetRandom(0, gSpriteWorldP->backRect.right-44),
- GetRandom(0, gSpriteWorldP->backRect.bottom-44) );
-
- do
- {
- SWSetSpriteMoveDelta(newSpriteP,
- GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta),
- GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta));
- } while (newSpriteP->horizMoveDelta == 0 || newSpriteP->vertMoveDelta == 0);
-
- if (kUseShadows == true)
- {
- // Create the shadow Sprite
- err = SWCloneSprite(gMasterBallSpriteP, &shadowSpriteP, NULL);
- FatalError(err);
-
- SWAddSprite(gShadowSpriteLayerP, shadowSpriteP);
- SWSetSpriteLocation(shadowSpriteP,
- newSpriteP->destFrameRect.left + kShadowOffset,
- newSpriteP->destFrameRect.top + kShadowOffset);
- SWSetSpriteDrawProc(shadowSpriteP, ShadowSpriteDrawProc);
-
- // Store a pointer to the shadow Sprite in the parent Sprite's userData field
- newSpriteP->userData = (long)shadowSpriteP;
- }
- else
- {
- newSpriteP->userData = 0L;
- }
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // RunAnimation
- ///--------------------------------------------------------------------------------------
-
- void RunAnimation(void)
- {
- unsigned long frames;
-
- SetCursor(&qd.arrow);
- HideCursor();
-
- // Make sure CopyBits, if used, doesn't try to colorize things
- SWSetPortToWindow(gSpriteWorldP);
- ForeColor(blackColor);
- BackColor(whiteColor);
-
- SWUpdateSpriteWorld(gSpriteWorldP, true);
-
- FatalError( SWStickyError() ); // Make sure no errors got past us during setup
-
- frames = 0;
- StartTimer(); // For FPS report later
-
- while (!Button())
- {
- SWProcessSpriteWorld(gSpriteWorldP);
- FatalError( SWStickyError() ); // Make sure no errors occurred during a MoveProc, etc.
- SWAnimateSpriteWorld(gSpriteWorldP);
-
- if (gSpriteWorldP->frameHasOccurred)
- frames++;
- }
-
- SWShowMenuBar((WindowPtr)gWindowP);
- ShowResults(frames); // Show FPS report
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CleanUp - This function is not really necessary, since the system will dispose
- // everything automatically when the program quits. However, if you sync the animation
- // to the VBL, you must either call SWDisposeSpriteWorld, or SWSyncSpriteWorldToVBL
- // with a value of false before your program quits, in order to remove the VBL task.
- ///--------------------------------------------------------------------------------------
-
- void CleanUp(void)
- {
- // Dispose the master sprite, since it isn't included in the SpriteWorld
- SWDisposeSprite(&gMasterBallSpriteP);
-
- // Dispose the SpriteWorld, including all sprites and layers currently in it
- SWDisposeSpriteWorld(&gSpriteWorldP);
- SWExitSpriteWorld();
-
- FlushEvents(everyEvent, 0);
- InitCursor();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // MyCleanUpFunction - called if an error occurs, to clean up before quitting
- ///--------------------------------------------------------------------------------------
-
- void MyCleanUpFunction( void )
- {
- SWShowMenuBar(gWindowP);
- RestoreControlStrip();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // BallSpriteMoveProc
- ///--------------------------------------------------------------------------------------
-
- SW_FUNC void BallSpriteMoveProc(SpritePtr ballSpriteP)
- {
- SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, ballSpriteP->vertMoveDelta);
-
- (void)SWBounceSprite(ballSpriteP);
- //(void)SWWrapSprite(ballSpriteP);
-
- // Move this Sprite's shadow, if there is one.
- if (ballSpriteP->userData != 0L)
- {
- SpritePtr shadowSpriteP = (SpritePtr)ballSpriteP->userData;
-
- SWMoveSprite(shadowSpriteP,
- ballSpriteP->destFrameRect.left + kShadowOffset - 5,
- ballSpriteP->destFrameRect.top + kShadowOffset);
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // ShadowSpriteDrawProc - uses the Sprite's region, but not image, to "tint" the
- // area underneath the Sprite the color you specify. Note: Brian's Utils would do this faster!
- ///--------------------------------------------------------------------------------------
-
- #define kShadowDarkness 0x5000 // Controls the shadow's darkness
-
- SW_FUNC void ShadowSpriteDrawProc(FramePtr srcFrameP, FramePtr dstFrameP,
- Rect* srcRect, Rect* dstRect)
- {
- #pragma unused(dstFrameP, srcRect)
- RGBColor col;
- PenState pnState;
- RgnHandle theRgn;
-
- theRgn = NewRgn();
- CopyRgn(srcFrameP->maskRgn, theRgn);
- OffsetRgn(theRgn, dstRect->left, dstRect->top);
- col.red = kShadowDarkness;
- col.green = kShadowDarkness;
- col.blue = kShadowDarkness;
- RGBForeColor(&col);
- GetPenState(&pnState);
- PenMode(subPin);
- PaintRgn(theRgn);
- PenMode(0);
- SetPenState(&pnState);
- DisposeRgn(theRgn);
- ForeColor(blackColor);
- }
-